﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI.Design;
using TA.Turbo.Controls;
using System.ComponentModel;
using System.Web.UI;
using System.ComponentModel.Design;

namespace TA.Turbo.Design
{
	/// <summary>
	/// Provides a user interface which can edit the contents of a <see cref="Repeater"/>.
	/// </summary>
	public class RepeaterDesigner : ControlDesigner
	{
		#region PUBLIC
		#region INIT
		#endregion
		#region EVENTS
		#endregion
		#region PROPS
		#endregion
		#region METHODS
		/// <summary>
		/// Retrieves the HTML markup to display the control and populates the collection with the current control designer regions.
		/// </summary>
		/// <param name="regions"></param>
		/// <returns></returns>
		public override string GetDesignTimeHtml(DesignerRegionCollection regions)
		{
			if (regions == null)
				return base.GetDesignTimeHtml(regions);

			this.AddRegion(regions, this.Control.Header);
			this.AddRegion(regions, this.Control.ViewItem);
			this.AddRegion(regions, this.Control.EditItem);
			this.AddRegion(regions, this.Control.Footer);

			return
				"<div>" +
					"<div style=\"font:bold 13px Verdana;color:Red;\">Header</div>" +
					"<div _designerRegion=\"0\"></div>" +
					"<div style=\"font:bold 13px Verdana;color:Red;\">ViewItem</div>" +
					"<div _designerRegion=\"1\"></div>" +
					"<div style=\"font:bold 13px Verdana;color:Red;\">EditItem</div>" +
					"<div _designerRegion=\"2\"></div>" +
					"<div style=\"font:bold 13px Verdana;color:Red;\">Footer</div>" +
					"<div _designerRegion=\"3\"></div>" +
				"</div>";
		}
		/// <summary>
		/// Returns the content for an editable region of the design-time view of the associated control.
		/// </summary>
		/// <param name="region"></param>
		/// <returns></returns>
		public override string GetEditableDesignerRegionContent(EditableDesignerRegion region)
		{
			RepeaterRegion region2 = region as RepeaterRegion;
			if (region2 != null)
			{
				ITemplate template = region2.Template;
				if (template != null)
				{
					IDesignerHost service = (IDesignerHost)base.Component.Site.GetService(typeof(IDesignerHost));
					return ControlPersister.PersistTemplate(template, service);
				}
			}
			return base.GetEditableDesignerRegionContent(region);
		}
		/// <summary>
		/// Specifies the content for an editable region of the control at design time.
		/// </summary>
		/// <param name="region"></param>
		/// <param name="content"></param>
		public override void SetEditableDesignerRegionContent(EditableDesignerRegion region, string content)
		{
			RepeaterRegion region2 = region as RepeaterRegion;
			if (region2 != null)
			{
				IDesignerHost service = (IDesignerHost)base.Component.Site.GetService(typeof(IDesignerHost));
				ITemplate template = ControlParser.ParseTemplate(service, content);
				region2.PropertyDescriptor.SetValue(region2.Object, template);
				region2.Template = template;
			}
		}
		#endregion
		#endregion
		#region INTERNAL
		#region DECLARATIONS
		private class RepeaterRegion : TemplatedEditableDesignerRegion
		{
			private object _object;
			private PropertyDescriptor _propertyDescriptor;
			private ITemplate _template;

			// Methods
			public RepeaterRegion(object obj, ITemplate template, PropertyDescriptor descriptor, TemplateDefinition definition)
				: base(definition)
			{
				this._template = template;
				this._object = obj;
				this._propertyDescriptor = descriptor;
				base.EnsureSize = true;
			}

			// Properties
			public object Object
			{
				get
				{
					return this._object;
				}
			}

			public PropertyDescriptor PropertyDescriptor
			{
				get
				{
					return this._propertyDescriptor;
				}
			}

			public ITemplate Template
			{
				get
				{
					return this._template;
				}
				set
				{
					this._template = value;
				}
			}

		}
		#endregion
		#region CONST/STATIC
		#endregion
		#region VARS
		#endregion
		#region PROPS
		/// <summary>
		/// Gets a value indicating whether the control designer uses a temporary preview control to generate the design-time HTML markup.
		/// </summary>
		protected override bool UsePreviewControl
		{
			get
			{
				return true;
			}
		}
		/// <summary>
		/// Gets the <see cref="DelayedLoadPanel"/> that's being designed.
		/// </summary>
		protected Repeater Control
		{
			get
			{
				return (Repeater)this.Component;
			}
		}
		#endregion
		#region METHODS
		#region INIT
		private void AddRegion(DesignerRegionCollection regions, MarkupTemplate template)
		{
			ITemplate t = null;
			if (template is SingleInstanceTemplate)
				t = ((SingleInstanceTemplate)template).Template;
			else if (template is MultipleInstanceTemplate)
				t = ((MultipleInstanceTemplate)template).Template;

			EditableDesignerRegion region =
				new RepeaterRegion(
					template,
					t,
					TypeDescriptor.GetProperties(template)["Template"],
					new TemplateDefinition(this, "Template", template, "Template"));
			regions.Add(region);
		}
		#endregion
		#region DATA
		#endregion
		#region HANDLERS
		#endregion
		#endregion
		#endregion

	}
}
